package cn.iocoder.yudao.im.controller.admin;


import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.im.domain.NoticeImGroup;
import cn.iocoder.yudao.im.domain.moudle.GroupInviteVO;
import cn.iocoder.yudao.im.service.NoticeImGroupService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * <p>
 * 群 前端控制器
 * </p>
 *
 * @author ZCC
 * @since 2024-07-02
 */
@RestController
@RequestMapping("/group")
public class NoticeImGroupController {

    @Resource
    private NoticeImGroupService groupService;


    /**
     * 创建群聊
     *
     * @param vo 群组数据
     * @return CommonResult
     */
    @PostMapping("/create")
    public CommonResult createGroup(@Valid @RequestBody NoticeImGroup vo) {
        return CommonResult.success(groupService.createGroup(vo));
    }


    /**
     * 修改群聊信息
     *
     * @param vo 群组数据
     * @return CommonResult
     */
    @PostMapping("/modify")
    public CommonResult modifyGroup(@Valid @RequestBody NoticeImGroup vo) {
        return CommonResult.success(groupService.modifyGroup(vo));
    }

    /**
     * 解散群聊
     *
     * @param groupId 群组id
     * @return CommonResult
     */
    @GetMapping("/delete")
    public CommonResult deleteGroup(@NotNull(message = "群聊id不能为空") @RequestParam("groupId") Long groupId) {
        groupService.deleteGroup(groupId);
        return CommonResult.success("");
    }


    /**
     * 查询单个群聊信息
     *
     * @param groupId 群组id
     * @return CommonResult
     */
    @GetMapping("/find")
    public CommonResult findGroup(@NotNull(message = "群聊id不能为空") @RequestParam("groupId") Long groupId) {
        return CommonResult.success(groupService.findById(groupId));
    }


    /**
     * 查询群聊列表
     *
     * @return CommonResult
     */
    @GetMapping("/list")
    public CommonResult findGroups() {
        return CommonResult.success(groupService.findGroups());
    }


    /**
     * 邀请进群
     *
     * @param vo 邀请用户数据
     * @return CommonResult
     */
    @PostMapping("/invite")
    public CommonResult invite(@Valid @RequestBody GroupInviteVO vo) {
        groupService.invite(vo);
        return CommonResult.success("");
    }


    /**
     * 查询群聊成员
     *
     * @param groupId 群组id
     * @return CommonResult
     */
    @GetMapping("/members")
    public CommonResult findGroupMembers(@NotNull(message = "群聊id不能为空") @RequestParam("groupId") Long groupId) {
        return CommonResult.success(groupService.findGroupMembers(groupId));
    }

    /**
     * 退出群聊
     *
     * @param groupId 群组id
     * @return CommonResult
     */
    @GetMapping("/quit")
    public CommonResult quitGroup(@NotNull(message = "群聊id不能为空") @RequestParam("groupId") Long groupId) {
        groupService.quitGroup(groupId);
        return CommonResult.success("");
    }


    /**
     * 将用户踢出群聊
     *
     * @param groupId 群组id
     * @param userId  用户id
     * @return CommonResult
     */
    @GetMapping("/kick")
    public CommonResult kickGroup(@NotNull(message = "群聊id不能为空") @RequestParam("groupId") Long groupId, @NotNull(message = "用户id不能为空") @RequestParam("userId") String userIds) {
        groupService.kickGroup(groupId, userIds);
        return CommonResult.success("");
    }

}
